package com.d3works.apps.bankpayment.web;

import com.d3works.apps.bankpayment.service.bankstatement.BankStatementLineService;
import com.d3works.apps.base.db.BankDetails;
import com.d3works.apps.base.db.repo.BankDetailsRepository;
import com.d3works.common.StringUtils;
import com.d3works.exception.service.TraceBackService;
import com.d3works.inject.Beans;
import com.d3works.meta.schema.actions.ActionView;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;
import java.time.LocalDate;
import java.util.Map;

public class BankStatementLineController {

  public void print(ActionRequest request, ActionResponse response) {
    try {
      LocalDate fromDate = LocalDate.parse(request.getContext().get("fromDate").toString());
      LocalDate toDate = LocalDate.parse(request.getContext().get("toDate").toString());
      Long bankDetail =
          Long.valueOf((Integer) ((Map) request.getContext().get("bankDetails")).get("id"));
      BankDetails bankDetails = Beans.get(BankDetailsRepository.class).find(bankDetail);
      String exportType = (String) request.getContext().get("exportTypeSelect");

      String fileLink =
          Beans.get(BankStatementLineService.class)
              .print(fromDate, toDate, bankDetails, exportType);
      if (StringUtils.notEmpty(fileLink)) {
        response.setView(ActionView.define("Bank statement lines").add("html", fileLink).map());
      }

    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }

    response.setReload(true);
  }
}
